package com.ruoyi.web.controller.liangshi;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.sql.LambdaQueryWrapperX;
import com.ruoyi.liangshi.domain.ArchItem;
import com.ruoyi.liangshi.domain.ArchItemClass;
import com.ruoyi.liangshi.domain.ArchItemUnit;
import com.ruoyi.liangshi.domain.AtchItem;
import com.ruoyi.liangshi.service.IArchItemClassService;
import com.ruoyi.liangshi.service.IArchItemService;
import com.ruoyi.liangshi.service.IArchItemUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author LiuJin
 * @Date: Create in 2023-11-14 14:08
 * @Description: this class is
 */
@RestController
@RequestMapping("/liangshi/atchItem")
public class ArchItemController extends BaseController {

    @Autowired
    private IArchItemService atchItemService;


    @Autowired
    private IArchItemClassService archItemClassService;

    @Autowired
    private IArchItemUnitService archItemUnitService;

    /**
     * 获取菜品列表
     * @param atchItem
     * @return
     */
    @GetMapping("/getAtchItemList")
    public TableDataInfo getAtchItemList(AtchItem atchItem) {
        startPage();
        List<ArchItem> atchItemList = atchItemService.list(
                new LambdaQueryWrapperX<ArchItem>()
                        .likeIfPresent(ArchItem::getName, atchItem.getName())
                        .eqIfPresent(ArchItem::getSmallClassId, atchItem.getSmallClassId()));
        Map<Long, String> archItemClassMaps = archItemClassService.list().stream().collect(Collectors.toMap(ArchItemClass::getId, ArchItemClass::getName));
        Map<Long, String> archItemUnitMaps = archItemUnitService.list().stream().collect(Collectors.toMap(ArchItemUnit::getId, ArchItemUnit::getName));
        for (ArchItem item : atchItemList) {
            String archItemClassName = archItemClassMaps.get(item.getSmallClassId());
            if (StrUtil.isNotBlank(archItemClassName)) {
                item.setBrandName(archItemClassName);
            }
            String archItemUnitName = archItemUnitMaps.get(item.getUnitId());
            if (StrUtil.isNotBlank(archItemUnitName)) {
                item.setUnitName(archItemUnitName);
            }
        }
        return getDataTable(atchItemList);
    }

    /**
     * 修改菜品信息，主要用于修改菜品难易程度
     *
     * @param atchItem
     * @return
     */
    @PutMapping("/updateItem")
    public AjaxResult updateItem(@RequestBody ArchItem atchItem) {
        atchItemService.updateById(atchItem);
        return success();
    }
}
